<?php
namespace frontend\controllers;

use common\models\Post;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;


/**
 * Logincontroller
 */
class SearchController extends Controller
{

    public $enableCsrfValidation = false;

    public function actionIndex($q, $type = 0)
    {


        if ($type == 1) {
            $diffTime = time() -  60 * 60 * 24;
        } else if ($type == 2) {
            $diffTime = time() - 60 * 60 * 24 * 7;
        } elseif ($type == 3) {
            $diffTime = time() - 60 * 60 * 24 * 90;
        }else{
            $diffTime =0;
        }
        $query = Post::find()->joinWith('cate')->orderBy(['post.id' => SORT_DESC])->asArray();
        //文章
        $post = clone $query;
        if ($q) {
            $post = $query->andFilterWhere(['like', 'post.title', $q])
                ->orFilterWhere(['like', 'post.content', $q])
                ->andWhere('post.create_at > :diffTime', [':diffTime' => $diffTime]);

        }
        $pages = new Pagination([
            'totalCount' => $post->count(),
            'defaultPageSize' => 10
        ]);
        $model = $post->offset($pages->offset)->limit($pages->limit)->all();


        return $this->render('index', [
            'model' => $model,
            'pages' => $pages,
            'q' => $q,
            'type' => $type,
            'count' =>  $post->count(),


        ]);
    }


}
